﻿Imports System.ServiceModel
Imports ajayumi.develop.win32.Transport.Wcf.Contracts
Imports ajayumi.develop.win32.Transport.Wcf.Services
Imports ajListView = ajayumi.develop.VBasic.Win.Controls.ListView
Imports ajColumnHeader = ajayumi.develop.VBasic.Win.Controls.ColumnHeader
Imports ajValueType = ajayumi.develop.VBasic.Win.Controls.ValueType

Public Class frmMain

#Region "variable"

    Private m_UploadFileService As ServiceHost = Nothing
    Private m_DownloadFileService As ServiceHost = Nothing

#End Region

#Region "private method"

    ''' <summary>
    ''' 设置状态栏标签的文本值
    ''' </summary>
    ''' <param name="control">状态栏标签控件</param>
    ''' <param name="text">文本值</param>
    Private Sub setText(ByVal control As ToolStripStatusLabel, ByVal text As String)
        If Me.StatusStrip1.InvokeRequired Then
            Me.StatusStrip1.Invoke(New Action(Of ToolStripStatusLabel, String)(AddressOf Me.setText), New Object() {control, text})
        Else
            control.Text = text
        End If
    End Sub

    ''' <summary>
    ''' 初始化服务
    ''' </summary>
    Private Sub initService()
        Me.m_UploadFileService = New ServiceHost(GetType(UploadFileService))
        UploadFileService.Lv = Me.lvUploadFile
        UploadFileService.Txt = Me.txtMsg
        UploadFileService.SyncContext = System.Threading.SynchronizationContext.Current
        With Me.m_UploadFileService
            AddHandler .Opened, AddressOf Me.UploadFileService_Opened
            AddHandler .Closed, AddressOf Me.UploadFileService_Closed
        End With

        Me.m_DownloadFileService = New ServiceHost(GetType(DownloadFileService))
        With Me.m_DownloadFileService
            AddHandler .Opened, AddressOf Me.DownloadFileService_Opened
            AddHandler .Closed, AddressOf Me.DownloadFileService_Closed
        End With
    End Sub

    ''' <summary>
    ''' 初始化上传文件列表控件 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub initLvUploadFile()
        Dim _GuidCH As New ajColumnHeader(Settings.GUID_KEY, "Guid", 100, HorizontalAlignment.Left, ajValueType.Text)
        Dim _FilePathCH As New ajColumnHeader(Settings.FILEPATH_KEY, "文件路径", 200, HorizontalAlignment.Left, ajValueType.Text)
        Dim _SavePathCH As New ajColumnHeader(Settings.SAVEPATH_KEY, "保存路径", 200, HorizontalAlignment.Left, ajValueType.Text)
        Dim _FileLenghtCH As New ajColumnHeader(Settings.FILELENGTH_KEY, "文件大小", 150, HorizontalAlignment.Center, ajValueType.Number)
        Dim _ProgressCH As New ajColumnHeader(Settings.PROGRESS_KEY, "进度", 100, HorizontalAlignment.Center, ajValueType.Number)
        Dim _StautsCH As New ajColumnHeader(Settings.STAUTS_KEY, "状态", 100, HorizontalAlignment.Center, ajValueType.Text)

        Me.lvUploadFile.Columns.AddRange(New ColumnHeader() {_GuidCH, _FilePathCH, _SavePathCH, _FileLenghtCH, _ProgressCH, _StautsCH})
    End Sub

#End Region

#Region "窗体相关事件"

    Private Sub frmMain_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        System.Environment.Exit(System.Environment.ExitCode)
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.initLvUploadFile()
    End Sub

    Private Sub tsmiStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiStart.Click, tsmiStart2.Click
        Me.initService()
        Me.m_UploadFileService.Open()
        Me.m_DownloadFileService.Open()
        Me.setText(Me.tsslbStauts, "服务已启动")

        Me.tsmiStart.Enabled = False
        Me.tsmiStop.Enabled = True
        Me.tsmiStart2.Enabled = False
        Me.tsmiStop2.Enabled = True
    End Sub

    Private Sub tsmiStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiStop.Click, tsmiStop2.Click
        Me.m_UploadFileService.Close()
        Me.m_DownloadFileService.Close()
        Me.setText(Me.tsslbStauts, "服务未启动")

        Me.tsmiStart.Enabled = True
        Me.tsmiStop.Enabled = False
        Me.tsmiStart2.Enabled = True
        Me.tsmiStop2.Enabled = False
    End Sub

    Private Sub frmVisible_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiHide.Click, tsmiShow.Click
        If Me.Visible = True Then
            Me.Hide()
            Me.NotifyIcon1.Visible = True
        Else
            Me.Show()
            Me.NotifyIcon1.Visible = False
        End If
    End Sub

    Private Sub tsmiExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiExit.Click, tsmiExit2.Click
        System.Environment.Exit(System.Environment.ExitCode)
    End Sub

#End Region

    Sub UploadFileService_Opened(ByVal sender As Object, ByVal e As EventArgs)
        ControlHelper.AppendText(Me.txtMsg, "上传服务已经启动" & vbCrLf)
    End Sub

    Sub UploadFileService_Closed(ByVal sender As Object, ByVal e As EventArgs)
        ControlHelper.AppendText(Me.txtMsg, "上传服务已经停止" & vbCrLf)
    End Sub

    Sub DownloadFileService_Opened(ByVal sender As Object, ByVal e As EventArgs)
        ControlHelper.AppendText(Me.txtMsg, "下载服务已经启动" & vbCrLf)
    End Sub

    Sub DownloadFileService_Closed(ByVal sender As Object, ByVal e As EventArgs)
        ControlHelper.AppendText(Me.txtMsg, "下载服务已经停止" & vbCrLf)
    End Sub


End Class
